package cn.zhentao.service.impl;

import cn.zhentao.mapper.*;
import cn.zhentao.pojo.*;
import cn.zhentao.service.IOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单信息表 服务实现类
 * </p>
 *
 * @author mp
 * @since 2025-06-16
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrderAssignmentsMapper orderAssignmentsMapper;
    @Autowired
    DeliveryMenMapper deliveryMenMapper;
    @Autowired
    UserAddressesMapper userAddressesMapper;
    @Autowired
    UsersMapper usersMapper;


    @Override
    public Page<Orders> findAllPage(QueryPage queryPage) {
        Page<Orders> page = new Page<>(queryPage.getCurrent(), queryPage.getSize());
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq(queryPage.getOrder_number() != null, "order_number", queryPage.getOrder_number());
        wrapper.eq(queryPage.getStatus() != null, "status", queryPage.getStatus());
        wrapper.eq(queryPage.getUserId()!= null,"user_id",queryPage.getUserId());
        ordersMapper.selectPage(page,wrapper);
        List<Orders> list = page.getRecords();
        for (Orders orders : list) {
            if(orders.getUserId()==queryPage.getUserId()){
                QueryWrapper<OrderAssignments> orderAssignmentsQueryWrapper = new QueryWrapper<>();
                List<OrderAssignments> orderAssignmentsList = orderAssignmentsMapper.selectList(orderAssignmentsQueryWrapper);
                for (OrderAssignments orderAssignments : orderAssignmentsList) {
                    DeliveryMen deliveryMen = deliveryMenMapper.selectById(orderAssignments.getDeliverymanId());
                    orders.setDeliveryname(deliveryMen.getName());
                    UserAddresses userAddresses = userAddressesMapper.selectById(orders.getAddressId());
                    orders.setMudidi3(userAddresses.getAddress());

                }
            }
        }
        return page;
    }

    @Override
    public Page<Orders> orderFind(QueryOrders queryOrders) {
//        System.err.println(queryOrders);
        Page<Orders>page = new Page<>(queryOrders.getCurrent(), queryOrders.getSize());
        QueryWrapper<Orders>wrapper=new QueryWrapper<>();
        wrapper.like(queryOrders.getOrderNumber() != null,"order_number",queryOrders.getOrderNumber());
//        wrapper.like(queryOrders.getUname()!=null,"uname",queryOrders.getUname());
//        wrapper.like(queryOrders.getDname()!=null,"dname",queryOrders.getDname());
        wrapper.like(queryOrders.getStatus()!=null,"status",queryOrders.getStatus());
        wrapper.between(queryOrders.getStartRegisterTime()!=null,"created_at",queryOrders.getStartRegisterTime(),queryOrders.getEndRegisterTime());
        ordersMapper.selectPage(page,wrapper);
        List<Orders> list = page.getRecords();
        for (Orders orders : list) {
            QueryWrapper<OrderAssignments> orderAssignmentsQueryWrapper=new QueryWrapper<>();
            List<OrderAssignments> orderAssignments = orderAssignmentsMapper.selectList(orderAssignmentsQueryWrapper);
            for (OrderAssignments orderAssignment : orderAssignments) {
                Users users = usersMapper.selectById(orders.getUserId());
                orders.setUname(users.getUsername());
                DeliveryMen deliveryMen = deliveryMenMapper.selectById(orderAssignment.getDeliverymanId());
                orders.setDname(deliveryMen.getName());
            }
        }
        List<Orders> list1 =  new ArrayList<>();
        if(queryOrders.getUname()!=null && queryOrders.getDname()!=null){
            for (Orders orders : list) {
                if(orders.getUname().equals(queryOrders.getUname()) && orders.getDname().equals(queryOrders.getDname())){
                    list1.add(orders);
                }
            }
            page.setRecords(list1);
            return page;
        }
        if(queryOrders.getUname()!=null){
            for (Orders orders : list) {
                if(orders.getUname().equals(queryOrders.getUname())){
                    list1.add(orders);
                }
            }
            page.setRecords(list1);
            return page;
        }
        if(queryOrders.getDname()!=null){
            for (Orders orders : list) {
                if(orders.getDname().equals(queryOrders.getDname())){
                    list1.add(orders);
                }
            }
            page.setRecords(list1);
            return page;
        }


        return page;
    }

    @Override
    public Orders selectByOrderNumber(String orderNumber) {
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq("order_number",orderNumber);
        Orders orders = ordersMapper.selectOne(wrapper);
        return orders;
    }
    @Autowired
    private VTaskPackageOrdersMapper vTaskPackageOrdersMapper;
    @Override
    public void completeOrdersByPackageId(String packageId) {
        // 2. 找到该任务包下所有订单ID
        List<VTaskPackageOrders> vOrders = vTaskPackageOrdersMapper.selectList(
                new QueryWrapper<VTaskPackageOrders>().eq("package_id", packageId)
        );
        if (vOrders.isEmpty()) return;
        List<Long> orderIds = vOrders.stream().map(VTaskPackageOrders::getOrderId).collect(Collectors.toList());
        int updatedOrders = ordersMapper.update(
                null,
                new UpdateWrapper<Orders>()
                        .in("id", orderIds)
                        .set("status", 3)

        );
    }

}

